home *** CD-ROM | disk | FTP | other *** search
- L i s t
- =======
-
- Documentation for module List
-
- Author: Michael Frieß [mif]
- Kernerstr. 22a
- 7000 Stuttgart 1
-
- Copyright remarks
- -----------------
-
- (C) Copyright 1988 by Michael Frieß. All rights reserved.
- The whole package (see chapter package) is public domain.
- All (source, documentation and code) is freely redistributable,
- as long as my name and the copyright notices remain intact, and
- only for non-commercial use. That means you can give it away, but
- don´t sell it.
- Commercial use without my explicit, written permission is not
- allowed. Contact me to get it and, perhaps, to pay some fee.
-
- Improvements and new ideas are welcome, as long as the changes are
- good documented inline and in the documentation files. I´would like
- to know, if you make major changes and repost it.
-
-
- Contents
- --------
-
- Package
- Introduction
- Generic Data Types
- New Data Type "List"
- List-Operators
- Example of Use
- What was my intention?
- Further generic types
- Referenced Literature
-
- Package
- -------
-
- The whole package "List" consist of following files:
-
- List.doc This documentation file
- List.dok German documentation
- List.def Definition module
- List.mod Implementation module
- List.sym Symbol file
- List.obj Compiled object code
- TestOfList.mod Test module (source)
- TestOfList Test module in executable form
-
-
- Introduction
- ------------
-
- Often you need basic data structures and operations on
- them, which are not implemented in Modula-II.
- But Modula-II supports development of generic data types.
- The best known example of generic data type is "File".
- You can import File and procedures operating on it from
- system module "FileSystem".
- The one thing, you have to know, is the abstract concept
- of files, not the realization.
- This module offers a generic type "List". So you have not
- to reinvent wheel, e.g. program your own list by pointers,
- every time, you need one. You just have to know the
- abstract concept of lists.
-
-
- Generic Data Types
- ------------------
-
- In Modula-II (as well as in most other high-level languages)
- you can create new types by declaration. New types can consist
- of structures and components, but every component type have to
- be known before declaration of the new type (either by
- declaration or as an simple data type - for example BOOLEAN).
- Modula-II knows four structure methods for building new types:
- ARRAY, SET, RECORD and POINTER.
- With last one you can create a large scale of new types.
- For example a list:
- TYPE listptr = POINTER TO list;
- list = RECORD
- item : T0;
- next : listptr
- END;
-
- To use this list you develop some procedures for initialization,
- insert an item, read an item and so on. All procedures are
- independent of your component type T0, but your type "list"
- depends on it.
- A generic data type now can be used with every(!) component
- type!
- The concept of Modula-II supports development of modules that
- present data capsules exporting a generic type and procedures
- working on it.
-
-
- New Data Type "List"
- --------------------
-
- The generic type "List" is a sort of linear list with only
- sequential access. Linear means that one element follows the
- other (as in a queue!), sequential means that the list can be
- inspected only by go from one element to its succeedor.
- In opposite to a sequence (I refer to [1]) data can also be
- inserted into list, not only appended at the end.
-
-
- List-Operators
- --------------
-
- In following operators and their abstract intention are listed.
- You can find more details in definition module.
-
- o Init
- Initialize a new empty list for further use.
-
- o Discard
- Discard a list, i.e. all elements and the list itself, and frees
- memory allocation.
-
- o Insert
- insert a new element to the given list. Insertion can be done at
- list´s end or behind current element.
-
- o Write
- overwrites content of the current element or appends an element, if
- executed on end of the given list.
-
- o Reset
- sets current element to beginning of the given list.
-
- o Read
- gets content of current element and next element becomes current
- element for further inspections.
-
- o Delete
- deletes current element and frees its memory allocation.
-
- o End
- determines, whether end of list is reached.
-
- o Do
- executes a specific action (given by parameter) on whole list or a
- part of it. "Do" begins with current element and terminate at end
- of the given list or by special end indicated by the user defined
- action.
-
-
- Example of Use
- --------------
-
- Using the type list is very simple:
-
- 1) Import list and needed operators:
-
- FROM List IMPORT list, Init, Insert, Read, Reset, Discard ...;
-
- 2) Define your element type:
- (for example:)
-
- TYPE address = RECORD
- name, prename
- street,
- city : ARRAY [1..30] OF CHAR;
- country : CHAR
- END;
-
- 3) Define a list variable and a variable containing your data:
-
- VAR l : list;
- Address : address;
-
- 4) Initialize list l:
-
- Init (l);
-
- (or with qualified export/import:)
-
- List.Init (l);
-
- 5) Insert data:
-
- WITH Address DO
- name := "Kant";
- prename := "Immanuel";
- street := "Alte Mühlengasse 5";
- city := "Leipzig";
- country := "G"; (* Germany *)
- END;
- Insert (l, Address);
-
- inserting further addresses for example by reading from file or user
- input
-
- 6) Reset list l for reading the content:
-
- Reset (l);
-
- 7) Read one element:
-
- Read (l, Address);
-
- or all elements:
-
- WHILE NOT End(l) DO
- Read (l, Address);
- - Display Address -
- END;
-
- or display all elements by define a procedure "Display" to
- display one address and use the imported procedure "Do":
-
- Reset (l); (* begin with top of list *)
- Do (l, Display);
-
- 8) Discard the list after use:
-
- Discard (l);
-
-
- Easy, isn´t it?
-
-
- What was my intention?
- ----------------------
-
- I wanted to understand how to implement generic data types and general
- operators in a module as a data capsule. Now I tried to present you
- these (not even newest) concepts of programming:
-
- o information hiding
- The items exported by the module can be used without knowing
- implementation. It´s a way of abstraction, what is important for
- large programs. Modula-II supports this by the concept of modules.
-
- o data capsule
- The new data type and all features (operators, functions,
- procedures and so on) are surrounded by one module containing them
- like a capsule. You can only access objects of the new type by
- the given operators.
-
- o opaque type
- Another feature of Modula-II, the opaque type, is used for
- information hiding. The compiler M2Amiga allows only pointer types
- to be opaque, but probably other compilers will do same way.
-
- o generic type
- A data type (data structure) is possible to be used with any element
- type.
- Caution: Implementation of generic types is possible in Modula-II,
- but no more type compatibility check is possible, because the
- unspecified type ARRAY OF WORD (or ARRAY OF BYTE in M2Amiga) is
- used for indicating the element type.
-
- o procedure type
- Procedure type in Modula-II is a powerful tool for software
- development. In this case it is used for separation of actions, that
- are executed on different data.
- One data represents the whole list - the other represents one
- element. So action on the whole list is implemented in module
- List, the other one manipulating the element data is user defined.
- Remark similarity of action and data!
- In case of M2Amiga it seems not to be possible to deactivate
- parameter check while compiling and there is no other way to use
- a generic parameter in a procedure type (Hey, what about some
- improvements of the language, so Modula-III?).
- The test module demonstrates the way you can use a generic parameter,
- but it doesn´t satisfy me.
-
-
- Further generic types
- ---------------------
-
- I have implemented other generic data types:
-
- o queue
- a simple FIFO-list
-
- o stack
- a simple LIFO-list
-
- o AVL-Tree
-
- Surely there are a lot of other possible generic types:
-
- o pipe
- There is a pipe-handler at a fish-disk (I don´t know the number).
- I would enjoy an implementation of a pipe-handler with whole
- Modula-II support as a generic type.
- The great advantage of a pipe is the simultaneous use by multiple
- processes. For example two processes write data into the pipe and
- one process reads data simultaneous.
- (I´m looking forward the module for fully support of amiga´s multi-
- tasking operating system!)
-
- o BTree
- Another sort of tree also described in [1].
-
- o Hash-Table
- Perhaps a hash-table is also a candidat for generic data type -
- I haven´t thought about it yet.
-
- I´m looking forward a lot of other implementations of generic data
- types. For free distribution on AMOK-Disks send your implementations
- to my address or just to any address of an AMOK-member.
-
-
- Referenced literature
- ---------------------
-
- [1] N.Wirth "Algorithmen und Datenstrukturen mit Modula-II"
- Teubner, Stuttgart 1986
-